package com.resou.reader.api;

import com.resou.reader.data.Injection;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiImp implements IApi {

    private volatile static ApiImp sApiImp;
    private Retrofit retrofit;

    private ApiImp() {
        initRetrofit();
    }

    public static ApiImp getInstance() {
        if (sApiImp == null) {
            synchronized (IApi.class) {
                if (sApiImp == null) {
                    sApiImp = new ApiImp();
                }
            }
        }
        return sApiImp;
    }

    private void initRetrofit() {
        retrofit = new Retrofit.Builder().baseUrl(ApiConstant.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(Injection.provideOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }

    public <T> T getService(Class<T> clazz) {
        return retrofit.create(clazz);
    }
}
